package com.wish.g01.financial.controller;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.g01.base.entity.JrProduct;
import com.wish.g01.base.entity.MBank;
import com.wish.g01.base.entity.SfContract;
import com.wish.g01.base.entity.SysRegion;
import com.wish.g01.base.service.IJrProductService;
import com.wish.g01.base.service.IMBankService;
import com.wish.g01.base.service.ISfContractService;
import com.wish.g01.base.service.ISysRegionService;
import com.wish.g01.common.security.utils.SecurityUtils;
import com.wish.g01.financial.dto.FinancialDto;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wish.g01.common.log.annotation.Log;
import com.wish.g01.common.log.enums.BusinessType;
import com.wish.g01.common.security.annotation.PreAuthorize;
import com.wish.g01.common.core.web.controller.BaseController;
import com.wish.g01.common.core.web.domain.AjaxResult;
import com.wish.g01.common.core.utils.poi.ExcelUtil;
import com.wish.g01.common.core.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 金融产品
 * @author wish
 * @date 2022-05-27
 */
@RestController
@RequestMapping("/product")
public class TJrProductController extends BaseController
{
    @Autowired
    private IJrProductService iJrProductService;

    @Autowired
    private ISysRegionService iSysRegionService;

    @Autowired
    private IMBankService imBankService;

    @Resource
    ISfContractService iSfContractService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize(hasPermi = "financial:product:list")
    @GetMapping("/list")
    public TableDataInfo list(FinancialDto financialDto)
    {
        startPage();
        QueryWrapper<JrProduct> jrProductQueryWrapper = new QueryWrapper<>();
        jrProductQueryWrapper.eq("jr_product_type",financialDto.getJrProductType());
        jrProductQueryWrapper.eq("apply_state",1);
        List<JrProduct> list = iJrProductService.list(jrProductQueryWrapper);
        return getDataTable(list);
    }


    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize(hasPermi = "system:product:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(iJrProductService.getById(id));
    }

//    /**
//     * 新增【请填写功能名称】
//     */
    @PreAuthorize(hasPermi = "financial:product:add")
//    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FinancialDto financialDto)
    {
//        资方姓名
        if(financialDto.getCompanyId()==1){
            financialDto.setCompanyName("久金所");
        }else {
            financialDto.setCompanyName("应花分期");
        }
        JrProduct jrProduct = new JrProduct();
        BeanUtils.copyProperties(financialDto,jrProduct);
//        创建者id
        Long userId = SecurityUtils.getUserId();
        jrProduct.setCreateUserid(userId);
//        创建时间
        jrProduct.setCreateTime(LocalDateTime.now());
//       审核状态
        jrProduct.setApplyState(0);
        boolean save = iJrProductService.save(jrProduct);
        if(!save){
            return AjaxResult.error("错误");
        }
        return AjaxResult.success();
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize(hasPermi = "system:product:edit")
//    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FinancialDto financialDto)
    {
        //        资方姓名
        if(financialDto.getCompanyId()==1){
            financialDto.setCompanyName("久金所");
        }else {
            financialDto.setCompanyName("应花分期");
        }
        JrProduct jrProduct = new JrProduct();
        BeanUtils.copyProperties(financialDto,jrProduct);
        Long userId = SecurityUtils.getUserId();
        jrProduct.setUpdateUserid(userId);
        jrProduct.setUpdateTime(LocalDateTime.now());
        return AjaxResult.success(iJrProductService.saveOrUpdate(jrProduct));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize(hasPermi = "system:product:remove")
//    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        List<Long> longs = Arrays.asList(ids);
        List<JrProduct> jrProducts = iJrProductService.listByIds(longs);
        ArrayList<JrProduct> jrProducts1 = new ArrayList<>();
        for (JrProduct jrProduct : jrProducts) {
            jrProduct.setDelFlg(true);
            jrProducts1.add(jrProduct);
        }
        boolean bool = iJrProductService.saveOrUpdateBatch(jrProducts1);
        return AjaxResult.success(bool);
    }

    /**
     * 省
     * @return
     */
    @GetMapping("getProvinceList")
    public AjaxResult getProvinceList(){
        QueryWrapper<SysRegion> sysRegionQueryWrapper = new QueryWrapper<>();
        sysRegionQueryWrapper.eq("PARENT_ID",1);
        List<SysRegion> list = iSysRegionService.list(sysRegionQueryWrapper);
        return AjaxResult.success(list);
    }

    /**
     * 银行
     * @return
     */
    @GetMapping("getBankList")
    public AjaxResult getBankList(){
        QueryWrapper<MBank> mBankQueryWrapper = new QueryWrapper<>();
        mBankQueryWrapper.eq("del_flg",0);
        List<MBank> list = imBankService.list();
        return AjaxResult.success(list);
    }

    /**
     * 查询租约保理列表
     * @return
     */
    @GetMapping("getLeaseList")
    public AjaxResult getLeaseList(){
        QueryWrapper<JrProduct> jrProductQueryWrapper = new QueryWrapper<>();
        jrProductQueryWrapper.eq("jr_product_type",3);
        jrProductQueryWrapper.eq("del_flg",0);
        List<JrProduct> list = iJrProductService.list(jrProductQueryWrapper);
        return  AjaxResult.success(list);
    }

    /**
     * 收房合同查询
     */
    @GetMapping("sfList")
    public AjaxResult sfList(){
        startPage();
        List<SfContract> list = iSfContractService.list();
        System.out.println("************"+list.toString());
        return AjaxResult.success(list);
    }

    /**
     * 查询金融产品列表
     */
    @GetMapping("/JrProductList")
    public TableDataInfo JrProductList()
    {
        startPage();
        List<JrProduct> list = iJrProductService.list();

        return getDataTable(list);
    }

    /**
     * 更改下拉框数据
     * @param id
     * @return
     */
    @GetMapping("listprodect")
    public JrProduct listprodect(String id){
        JrProduct listprodect = iJrProductService.getById(id);
        System.out.println(listprodect);
        return listprodect;
    }
}
